Option Explicit
Dim doc
Set doc = application.ActiveDocument
main

' Function to validate document
Private Function ValidateServer(docObj)
    Dim key, licenseServer, licenseToken
    key = docObj.Validate(0)
    Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
    licenseToken = licenseServer.GetToken(key)
    Set licenseServer = nothing
    'On Error Resume Next
    Err.Clear
    docObj.Validate(licenseToken)
    If Err Then
        ValidateServer = 0    
    Else 
        ValidateServer = 1
    End If
End Function

sub GetUserLicence(docObj)

	If ValidateServer(docObj) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If
	
end sub

'----------------------------------------------------------------
sub main()
	GetUserLicence(doc)
	dim curunit
	dim usrcmd
	dim mmw
	usrcmd = "cw "
	curunit = doc.currentunit
	if curunit = 2 then usrcmd = usrcmd & "2.95"
	mmw = 2.95/39.37
	if curunit = 4 then usrcmd = usrcmd & mmw
	application.gui.processkeyin(usrcmd)
end sub
'====================================================

